package com.cn.goodsadmin.controller;

import com.cn.goodsadmin.po.Discard;
import com.cn.goodsadmin.service.DisCardService;
import com.cn.goodsadmin.util.ErrorMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@CrossOrigin(origins = {"http://10.10.10.143:8020"}, maxAge = 3600)
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
public class DisCardController {

    @Autowired
    DisCardService disCardService;

    @LoginRequired
    @RequestMapping(value = "/getalldiscard", method = RequestMethod.GET,params = {"appid"})
    @ResponseBody
    public SupplyMap getAllDisCards(int appid) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            List<Discard> discards = disCardService.findAllDisCard(appid);
            if (discards != null) {
                supplyMap.put("discards", discards);
            }
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/adddiscard", method = RequestMethod.POST, produces = "application/json;charset=utf-8",
            consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap addDisCard(@RequestBody Discard discard){
        SupplyMap supplyMap=new SupplyMap();
        try{
            disCardService.save(discard);
        }catch (Exception e){
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/updatediscard", method = RequestMethod.POST, produces = "application/json;charset=utf-8",
            consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap updateCoffeeInfo(@RequestBody Discard discard) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            disCardService.update(discard);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/deletediscard", method = RequestMethod.POST, produces = "application/json;charset=utf-8", consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap deleteCoffeeInfo(@RequestBody Discard discard){
        SupplyMap supplyMap=new SupplyMap();
        try{
            disCardService.delete(discard);
        }catch (Exception e){
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

}
